#ifndef YASDOWNLOADAGENT_H_
#define YASDOWNLOADAGENT_H_

#include <string>
#include <ctime>
#include <sstream>

#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>

#include "YasLogger.h"
#include "YasDownloadConfig.h"
#include "Yas.h"

namespace yas{

  /**
   * @class class YasDownloadAgent
   *
   * @author Wesley Mesquita (wesleymesquita@gmail.com)
   *
   * @brief YasDownloadAgent just can download data from web using HTTP
   *
   */
  class YasDownloadAgent: public YasCommon
  {
  public:
	YasDownloadAgent(const YasDownloadConfig *const conf);

    virtual ~YasDownloadAgent();

    int downloadSync(const std::string &fullUrl);

    std::string getDownloadedContent();

    void flushContent();

    void setDownloadConfig(const YasDownloadConfig *const config);

    const YasDownloadConfig* getDownloadConfig() const;

    int processConfiguration();

  private:
    const static unsigned long int MAXDATASIZE = 20000;

    const YasDownloadConfig *const config;

    std::string target_url;

    int port;

    bool connected;

    int socketfd;

    std::stringstream contentStream;

    int getSocketFd() const;
    int setSocketFd(int socketFd);
    int sendSocketData(const std::string & data);
    int recvSocketData();
    int connectSocket();
    void closeSocket();

    bool isConnected() const
    {
    	return connected;
    }

    void setIsConnected(bool connected)
    {
    	this->connected = connected;
    }

    std::stringstream& getContentStream();

    std::string createHttpHeader(
        const std::string & httpMethod,
        const std::string & url,
        const std::string & httpVersion,
        const std::string & acceptClause,
        const std::string & acceptEncoding,
        const std::string & userAgent,
        const std::string & connection
    ) const;

    std::string getPort() const;
    std::string getTargetUrl() const;

  };
}

#endif
